import requests
from bs4 import BeautifulSoup
def gethtml(url):
    try:
        re = requests.get(url, timeout=10)
        re.raise_for_status()
        re.encoding = "utf-8"
        print("获取成功")
        return re.text
    except:
        print("获取失败")
        return ""
def gethtmldata(html):
    soup=BeautifulSoup(html,"html.parser")
    div_ls=soup.find_all("div",{"class":"newbox_right"})
    all_ls=[]
    h2_ls=div_ls[0].find("h2")
    div_news=div_ls[0].find_all("div",{"class":"newslist"})
    for li in div_news[0].find_all("li"):
        ls=[]
        span_ls=li.find("span")
        # print(span_ls.string)
        ls.append(span_ls.string)
        a1=li.find("a")
        # print(a1.attrs["href"])
        # print(a1.attrs["title"])
        ls.append(a1.attrs["title"])
        ls.append(a1.attrs["href"])
        all_ls.append(ls)
        ls=[]
    return all_ls
def writehtmldata(ls):
    with open(r"九江职业技术学院.csv","w")as f:
        for line in ls:
            f.write(",".join(line)+"\n")

def main():
    url="http://www.jvtc.jx.cn/"
    html=gethtml(url)
    ls=gethtmldata(html)
    writehtmldata(ls)


if __name__ == '__main__':
    main()